package com.etime.perfectschool.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.perfectschool.mapper.NewsCommentsMapper;
import com.etime.perfectschool.mapper.NewsPhotoMapper;
import com.etime.perfectschool.mapper.SchoolNewsMapper;
import com.etime.perfectschool.pojo.NewsComments;
import com.etime.perfectschool.pojo.NewsPhoto;
import com.etime.perfectschool.pojo.SchoolNews;
import com.etime.perfectschool.service.SchoolNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class SchoolNewsServiceImpl implements SchoolNewsService {

    @Autowired
    private SchoolNewsMapper schoolNewsMapper;

    @Autowired
    private NewsPhotoMapper newsPhotoMapper;
    @Autowired
    private NewsCommentsMapper newsCommentsMapper;



    @Override
    public Page<SchoolNews> getAllSchoolNews(int current, int size) {
        Page<SchoolNews> page = new Page<>(current,size);
        return schoolNewsMapper.getAllSchoolNews(page);
    }

    @Override
    public int addSchoolNews(SchoolNews schoolNews) {
        schoolNewsMapper.addSchoolNews(schoolNews);
        return schoolNews.getNewsId();
    }

    @Override
    public boolean deleteSchoolNews(int newsid) {
        boolean res=false;
        List<NewsComments> allNewsComments = newsCommentsMapper.getAllNewsComments(newsid);
        List<NewsPhoto> allNewsPhotoByNewsId = newsPhotoMapper.getAllNewsPhotoByNewsId(newsid);
        if(allNewsComments!=null)
        {
            newsCommentsMapper.deleteNewsCommentsBynewId(newsid);
            if(allNewsPhotoByNewsId!=null)
            {
                newsPhotoMapper.deleteNewsPhotoById(newsid);
            }
        }else {
            if(allNewsPhotoByNewsId!=null)
            {
                newsPhotoMapper.deleteNewsPhotoById(newsid);
            }
        }
        res= schoolNewsMapper.deleteById(newsid)==0?false:true;
        return res;
    }

    @Override
    public boolean likeSchoolNews(SchoolNews schoolNews) {
        return schoolNewsMapper.updateById(schoolNews)==0?false:true;
    }

    @Override
    public SchoolNews getSchoolNews(int newsId) {
        return schoolNewsMapper.selectById(newsId);
    }

    @Override
    public boolean updateSchoolNews(SchoolNews schoolNews) {
        return schoolNewsMapper.updateById(schoolNews)==0?false:true;
    }
}
